package contact.domain;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class DAOContact extends HibernateDaoSupport{
	Session session;


	public DAOContact(){
		try{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
		} catch(HibernateException e){
			System.out.println("ERROR : "+e.getMessage());
		}
	}


	public Long create(Contact contact){
		//return (Long) getHibernateTemplate().save(contact);
		Transaction transaction = session.beginTransaction();
		Long ret =  (Long) session.save(contact);
		transaction.commit();
		return ret;
	}

	public Contact read(Long id){
		return (Contact) session.get(Contact.class, id);
	}

	public List<Contact> readAll(){
		Query query = session.createQuery("from Contact");
		return query.list();
		/*HibernateTemplate ht = getHibernateTemplate();
		if (ht == null) System.out.println("NULLLLLLLLLLLLLLLLLLL");
		return getHibernateTemplate().find("from Contact");*/
	}

	public void update(Contact contact){
		Transaction transaction = session.beginTransaction();
		session.update(contact);
		transaction.commit();
	}

	public void delete (Contact contact){
		Transaction transaction = session.beginTransaction();
		for (PhoneNumber phone : contact.getProfiles()){
			session.delete(phone);
		}
		session.delete(contact);
		transaction.commit();
	}

	public List readAllByContactCriteria(String field, String value){
		Criteria criteria = session.createCriteria(Contact.class);
		criteria.add(Restrictions.like(field, "%" + value + "%"));
		return criteria.list();
	}
	

	
	
	
	
	@Deprecated
	public void addContact(String firstname, String lastname, String email, String street, String city, String zip, String country, String m, String h, String j){
		Transaction transaction = session.beginTransaction();
		Contact contact = new Contact(firstname, lastname, email);
		Address address = new Address(street,city,zip,country);
		contact.setAddress(address);
		session.save(contact);
		PhoneNumber mobile = new PhoneNumber("MOBILE", m);
		contact.getProfiles().add(mobile);
		mobile.setContact(contact);
		session.save(mobile);
		session.flush();
		PhoneNumber home = new PhoneNumber("HOME", h);
		contact.getProfiles().add(home);
		home.setContact(contact);
		session.save(home);
		PhoneNumber job = new PhoneNumber("JOB", j);
		contact.getProfiles().add(job);
		job.setContact(contact);
		session.save(job);
		transaction.commit();
	}

	@Deprecated
	public void deleteContact(Long id){
		Transaction transaction = session.beginTransaction();
		Contact contact = new Contact();
		session.load(contact, id);
		for (PhoneNumber phone : contact.getProfiles()){
			session.delete(phone);
		}
		session.delete(contact);
		transaction.commit();
	}

	@Deprecated
	public Contact getContact(Long id){
		return (Contact) session.load(Contact.class, id);
	}

	@Deprecated
	public List getAllContact(){
		Query query = session.createQuery("from Contact");
		return query.list();
	}

	@Deprecated
	public void updateContact(Long id, String firstname, String lastname, String email, String street, String city, String zip, String country, String mobile, String home, String job){
		Transaction transaction = session.beginTransaction();
		Contact contact = new Contact();
		session.load(contact, id);
		contact.setFirstName(firstname);
		contact.setLastName(lastname);
		contact.setEmail(email);
		contact.getAddress().setStreet(street);
		contact.getAddress().setCity(city);
		contact.getAddress().setZip(zip);
		contact.getAddress().setCountry(country);
		for(PhoneNumber phone : contact.getProfiles()){
			if (phone.getPhoneKind().equals("MOBILE")) phone.setPhoneNumber(mobile);
			if (phone.getPhoneKind().equals("HOME")) phone.setPhoneNumber(home);
			if (phone.getPhoneKind().equals("JOB")) phone.setPhoneNumber(job);
		}
		session.update(contact);
		transaction.commit();
	}

	@Deprecated
	public List getAllContactByFirstName(String firstname){
		Query query = session.createQuery("from Contact as contact where contact.firstName='"+firstname+"'");
		return query.list();
	}
	
	@Deprecated
	public List getAllContactByCriteria(String field, String value){
		Query query = session.createQuery("from Contact as contact where contact."+field+"='"+value+"'");
		return query.list();
	}
}
